package com.micromall.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.micromall.common.base.BaseController;
import com.micromall.common.utils.Dict;
import com.micromall.common.utils.PageInfo;
import com.micromall.model.BusiCartDetailItem;
import com.micromall.service.IBusiCartDetailItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * ${table.comment}  前端控制器
 * </p>
 * @author Dante Fung
 * @since 2017-07-26
 */
@Controller
@RequestMapping("/busiCartDetailItem")
public class BusiCartDetailItemController extends BaseController {
    
    @Autowired private IBusiCartDetailItemService busiCartDetailItemService;
    
    @RequestMapping("/manager")
    public String manager() {
        return "admin/busiCartDetailItem/busiCartDetailItemList";
    }


    @PostMapping("/dataGrid")
    @RequestMapping
    public PageInfo dataGrid(BusiCartDetailItem busiCartDetailItem, Integer page, Integer rows, String sort,String order) {
        EntityWrapper<BusiCartDetailItem> ew = new EntityWrapper<BusiCartDetailItem>();
        ew.setEntity(busiCartDetailItem);
        Page<BusiCartDetailItem> pages = getPage(page, rows, sort, order);
        pages = busiCartDetailItemService.selectPage(pages,ew);
        return pageToPageInfo(pages);
    }
    
    /**
     * 添加页面
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "admin/busiCartDetailItem/busiCartDetailItemAdd";
    }

    /**
     * 添加
     * @param 
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(BusiCartDetailItem busiCartDetailItem) {
        boolean b = busiCartDetailItemService.insert(busiCartDetailItem);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        BusiCartDetailItem busiCartDetailItem = new BusiCartDetailItem();
        busiCartDetailItem.setId(id);
        boolean b = busiCartDetailItemService.updateById(busiCartDetailItem);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(Model model, Long id) {
        BusiCartDetailItem busiCartDetailItem = busiCartDetailItemService.selectById(id);
        model.addAttribute("busiCartDetailItem", busiCartDetailItem);
        return "admin/busiCartDetailItem/busiCartDetailItemEdit";
    }

    /**
     * 编辑
     * @param 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(BusiCartDetailItem busiCartDetailItem) {
        boolean b = busiCartDetailItemService.updateById(busiCartDetailItem);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }

    /**
     * 批量删除
     * @param request
     * @return
     */
    @RequestMapping("/batch_delete")
    @ResponseBody
    public Object batch_delete(HttpServletRequest request) {
        String ItemIds = request.getParameter("ItemIds");
        String[] ids = ItemIds.split(",");
        //修改状态为无效
        boolean b = false;
        if( null != ids && ids.length >0 ){
            for (String id: ids) {
                BusiCartDetailItem busiCartDetailItem = new BusiCartDetailItem();
                EntityWrapper<BusiCartDetailItem> ew = new EntityWrapper<>();
                busiCartDetailItem.setId(id);
                ew.setEntity(busiCartDetailItem);
                busiCartDetailItem = busiCartDetailItemService.selectOne(ew);
                busiCartDetailItem.setStandy3(Dict.YesOrNo.No.getCode());
                 b = busiCartDetailItemService.updateById(busiCartDetailItem);
            }
        }
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

}
